package com.zx.dao.impl;

import com.zx.bean.Express;
import com.zx.bean.IndexIndastrial;
import com.zx.dao.IndexIndastrialDao;
import com.zx.util.DruidUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class IndexIndastrialImpl implements IndexIndastrialDao {
    private static final String SQL_INSERT="insert into index_indastrial" +
            "(FIRM_NAME,FIRM_NO,DATA_YEAR,DATA_MONTH,SAFTY,SAFTY_GRADE,COMPLEX_USE,COMPLEX_USE_GRADE," +
            "WEATHER,WEATHER_GRADE,EEUE,EEUE_GRADE,IT_EQUIPMENT,IT_EQUIPMENT_GRADE,COOLING,COOLING_GRADE," +
            "OTHER_EQUIPMENT,OTHER_EQUIPMENT_GRADE,POWER_SYSTEM,POWER_SYSTEM_GRADE,ENERGY_SAVING_TEAM,ENERGY_SAVING_TEAM_GRADE," +
            "WORKING_SYSTEM,WORKING_SYSTEM_GRADE,ORTHER_SYSTEM,ORTHER_SYSTEM_GRADE,NEW_TYPE_SAVING_TECHNOLOGY," +
            "NEW_TYPE_SAVING_TECHNOLOGY_GRADE,ENVIRONMENTAL_PROTECTION_TECHNOLOGY,ENVIRONMENTAL_PROTECTION_TECHNOLOGY_GRADE," +
            "ENVIRONMENTAL_PROTECTION_PRODUCT,ENVIRONMENTAL_PROTECTION_PRODUCT_GRADE) " +
            "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";
    private  static  final String  SQL_FIND_LIMIT="select * from index_indastrial limit ?,?";  //分页查询全部
    private  static  final String  SQL_FIND_ALL="select * from index_indastrial"; //查询全部


    @Override
    /**
    * @Author: 吴澳
    * @Description:存储数据
    * @Param:* @param entity
    * @return: boolean
    * @Date: 2022/3/20
    */
    public boolean insert(IndexIndastrial entity) {
        //连接的获取
        Connection connection= DruidUtil.getConnection();
        PreparedStatement preparedStatement=null;

        try {
            preparedStatement=connection.prepareStatement(SQL_INSERT);
            preparedStatement.setString(1,entity.getFirmName());
            preparedStatement.setString(2,entity.getFirmNo());
            preparedStatement.setInt(3,entity.getDataYear());
            preparedStatement.setInt(4,entity.getDataMonth());
            preparedStatement.setString(5,entity.getSafty());
            preparedStatement.setInt(6,entity.getSaftyGrade());
            preparedStatement.setString(7,entity.getComplexUse());
            preparedStatement.setInt(8,entity.getComplexUseGrade());
            preparedStatement.setString(9,entity.getWeather());
            preparedStatement.setInt(10,entity.getWeatherGrade());
            preparedStatement.setString(11,entity.getEEUE());
            preparedStatement.setInt(12,entity.getEEUEGrade());
            preparedStatement.setString(13,entity.getITEquipment());
            preparedStatement.setInt(14,entity.getITEquipmentGrade());
            preparedStatement.setString(15,entity.getCooling());
            preparedStatement.setInt(16,entity.getCoolingGrade());
            preparedStatement.setString(17,entity.getOtherEquipment());
            preparedStatement.setInt(18,entity.getotherEquipmentGrade());
            preparedStatement.setString(19,entity.getPowerSystem());
            preparedStatement.setInt(20,entity.getPowerSystemGrade());
            preparedStatement.setString(21,entity.getEnergySavingTeam());
            preparedStatement.setInt(22,entity.getEnergySavingTeamGrade());
            preparedStatement.setString(23,entity.getWorkingSystem());
            preparedStatement.setInt(24,entity.getWorkingSystemGrade());
            preparedStatement.setString(25,entity.getOtherSystem());
            preparedStatement.setInt(26,entity.getOtherSystemGrade());
            preparedStatement.setString(27,entity.getNewTypeSavingTechnology());
            preparedStatement.setInt(28,entity.getNewTypeSavingTechnologyGrade());
            preparedStatement.setString(29,entity.getEnvironmentalProtectionTechnology());
            preparedStatement.setInt(30,entity.getEnvironmentalProtectionTechnologyGrade());
            preparedStatement.setString(31,entity.getEnvironmentalProtectionProduction());
            preparedStatement.setInt(32,entity.getEnvironmentalProtectionProductionGrade());
            return  preparedStatement.executeUpdate()>0?true:false;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DruidUtil.close(connection,preparedStatement,null);
        }
        return false;
    }

    @Override
    public List<IndexIndastrial> findAll(boolean limit, int offset, int pageNumber) {
        Connection connection=DruidUtil.getConnection();
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        ArrayList<IndexIndastrial> data=new ArrayList<>();
        try {
            if(limit){
                preparedStatement=connection.prepareStatement(SQL_FIND_LIMIT);
                preparedStatement.setInt(1,offset);
                preparedStatement.setInt(2,pageNumber);
            }else{
                preparedStatement=connection.prepareStatement(SQL_FIND_ALL);
            }
            resultSet=preparedStatement.executeQuery();
            while(resultSet.next()){
                IndexIndastrial entity=new IndexIndastrial();
                 int id=resultSet.getInt("ID");entity.setId(id);
                 String firmName=resultSet.getString("FIRM_NAME");entity.setFirmName(firmName);
                 String firmNo=resultSet.getString("Firm_NO");entity.setFirmNo(firmNo);
                 int DataYear=resultSet.getInt("DATA_YEAR");entity.setDataYear(DataYear);
                 int DataMonth=resultSet.getInt("DATA_MONTH");entity.setDataMonth(DataMonth);
                 String safty=resultSet.getString("SAFTY");entity.setSafty(safty);
                 int saftyGrade=resultSet.getInt("SAFTY_GRADE");entity.setSaftyGrade(saftyGrade);
                 String complexUse=resultSet.getString("COMPLEX_USE");entity.setComplexUse(complexUse);
                 int complexUseGrade=resultSet.getInt("COMPLEX_USE_GRADE");entity.setComplexUseGrade(complexUseGrade);
                 String weather=resultSet.getString("WEATHER");entity.setWeather(weather);
                 int weatherGrade=resultSet.getInt("WEATHER_GRADE");entity.setWeatherGrade(weatherGrade);
                 String EEUE=resultSet.getString("EEUE");entity.setEEUE(EEUE);
                 int EEUEGrade=resultSet.getInt("EEUE_GRADE");entity.setEEUEGrade(EEUEGrade);
                 String ITEquipment=resultSet.getString("IT_EQUIPMENT");entity.setITEquipment(ITEquipment);
                 int ITEquipmentGrade=resultSet.getInt("IT_EQUIPMENT_GRADE");entity.setITEquipmentGrade(ITEquipmentGrade);
                 String cooling=resultSet.getString("COOLING");entity.setCooling(cooling);
                 int coolingGrade=resultSet.getInt("COOLING_GRADE");entity.setCoolingGrade(coolingGrade);
                 String OtherEquipment=resultSet.getString("OTHER_EQUIPMENT");entity.setOtherEquipment(OtherEquipment);
                 int otherEquipmentGrade=resultSet.getInt("OTHER_EQUIPMENT_GRADE");entity.setOtherEquipmentGrade(otherEquipmentGrade);
                 String PowerSystem=resultSet.getString("POWER_SYSTEM");entity.setPowerSystem(PowerSystem);
                 int powerSystemGrade=resultSet.getInt("POWER_SYSTEM_GRADE");entity.setPowerSystemGrade(powerSystemGrade);
                 String energySavingTeam=resultSet.getString("ENERGY_SAVING_TEAM");entity.setEnergySavingTeam(energySavingTeam);
                 int energySavingTeamGrade=resultSet.getInt("ENERGY_SAVING_TEAM_GRADE");entity.setEnergySavingTeamGrade(energySavingTeamGrade);
                 String workingSystem=resultSet.getString("WORKING_SYSTEM");entity.setWorkingSystem(workingSystem);
                 int workingSystemGrade=resultSet.getInt("WORKING_SYSTEM_GRADE");entity.setWorkingSystemGrade(workingSystemGrade);
                 String otherSystem=resultSet.getString("OTHER_SYSTEM");entity.setOtherSystem(otherSystem);
                 int otherSystemGrade=resultSet.getInt("OTHER_SYSTEM_GRADE");entity.setOtherSystemGrade(workingSystemGrade);
                 String newTypeSavingTechnology=resultSet.getString("NEW_TYPE_SAVING_TECHNOLOGY");entity.setNewTypeSavingTechnology(newTypeSavingTechnology);
                 int newTypeSavingTechnologyGrade=resultSet.getInt("NEW_TYPE_SAVING_TECHNOLOGY_GRADE");entity.setNewTypeSavingTechnologyGrade(newTypeSavingTechnologyGrade);
                 String environmentalProtectionTechnology=resultSet.getString("ENVIRONMENTAL_PROTECTION_TECHNOLOGY");entity.setEnvironmentalProtectionTechnology(environmentalProtectionTechnology);
                 int environmentalProtectionTechnologyGrade=resultSet.getInt("ENVIRONMENTAL_PROTECTION_TECHNOLOGY_GRADE");entity.setEnvironmentalProtectionTechnologyGrade(environmentalProtectionTechnologyGrade);
                 String environmentalProtectionProduction=resultSet.getString("ENVIRONMENTAL_PROTECTION_PRODUCTION");entity.setEnvironmentalProtectionProduction(environmentalProtectionProduction);
                 int     environmentalProtectionProductionGrade=resultSet.getInt("ENVIRONMENTAL_PROTECTION_PRODUCTION");entity.setEnvironmentalProtectionProductionGrade(environmentalProtectionProductionGrade);
                data.add(entity);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DruidUtil.close(connection,preparedStatement,resultSet);
        }

        return data;
    }
}
